import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzButtonModule, NzButtonComponent } from 'ng-zorro-antd/button';
import { NzFormatEmitEvent } from 'ng-zorro-antd/core';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { DelonFormModule, SFSchema, SFComponent, WidgetRegistry, SFSchemaEnumType } from '@delon/form';
import { LbAAC001WidgetComponent } from '@shared';
import { HttpService, SupDic, GridComponent } from 'lbf';
import { Subject } from 'rxjs';
import { STColumn, STColumnTag, STColumnBadge } from '@delon/abc';

@Component({
  selector: 'app-ywtx-yjszxwh',
  templateUrl: './yjszxwh.component.html',
  styleUrls: ['./yjszxwh.less']
})
export class YwtxYjszxwhComponent implements OnInit {
  style = {height: (window.outerHeight-330)+'px',overflow: 'auto'}

  showSchema: SFSchema = {
    properties: {
      DAD017: {
        type: 'string',
        title: '事项主键',
        ui:
        {
          hidden: true
        }

      },
      DAD004: {
        type: 'string',
        title: '权力编码',
        ui: {
          widget: 'text',
          grid: {
            span: 12
          }
        }
      },
      DAD031: {
        type: 'string',
        title: '事项名称',
        ui: {
          widget: 'text',
          grid: {
            span: 12
          }
        }
      },
      DAD032: {
        type: 'string',
        title: '对象主体',
        enum: this.supDic.getSFDic('DAD032'),
        readOnly: true,
        ui: {
          widget: 'radio',
          grid: {
            span: 12
          }
        }
      },

      DGB010: {
        type: 'string',
        title: '所属部门',
        ui: {
          widget: 'text',
          grid: {
            span: 12
          }
        }
      },

    },
    ui: {
      spanLabelFixed: 90,
      grid: {
        span: 24
      }
    }
  };

  updateSchema: SFSchema = {
    properties: {
      DAD017: {
        type: 'string',
        title: '事项主键',
        ui:
        {
          hidden: true
        }

      },
      DAD031: {
        type: 'string',
        title: '事项名称'
      },
      DAD032: {
        type: 'string',
        title: '对象主体',
        enum: this.supDic.getSFDic('DAD032'),
        ui: {
          widget: 'radio',

        },
        default: '1'
      },
      DAD004: {
        type: 'string',
        title: '权力编码',
      },
      DGB010: {
        type: 'string',
        title: '所属部门',
        enum: this.supDic.getSFDic('MYBM'),
        ui: {
          widget: 'select',
        }
      },

    },
    ui: {
      spanLabelFixed: 90,
      grid: {
        span: 24
      }
    },
    required: ['DAD004', 'DGB010', 'DAD031', 'DAD032', 'DAD011']
  }
  oneAddSchema:SFSchema ={
    properties: {
      DAD017: {
        type: 'string',
        title: '事项主键',
        ui:
        {
          hidden: true
        }

      },
      DAD031: {
        type: 'string',
        title: '事项名称'
      },
      DAD032: {
        type: 'string',
        title: '对象主体',
        enum: this.supDic.getSFDic('DAD032'),
        ui: {
          widget: 'radio',

        },
        default: '1'
      },
      DAD011: {
        type: 'string',
        title: '事项类型',
        /*
        readOnly:true,
        enum: this.supDic.getSFDic('DAD011'),
        ui: {
          widget: 'select',
          
        },
        */
        ui:{
          hidden: true,
        },
        default: '1',
      },
      DAD004: {
        type: 'string',
        title: '权力编码',
      },
      DGB010: {
        type: 'string',
        title: '所属部门',
        enum: this.supDic.getSFDic('MYBM'),
        ui: {
          widget: 'select',

        }
      },

    },
    ui: {
      spanLabelFixed: 90,
      grid: {
        span: 24
      }
    },
    required: ['DAD004', 'DGB010', 'DAD031', 'DAD032', 'DAD011']
  }

  BADGE: STColumnBadge = {
    1: { text: '企业', color: 'success' },
    2: { text: '自然人', color: 'processing' },
  };


  TAG: STColumnTag = {
    0: { text: '0', color: 'purple' },
    1: { text: '1', color: 'green' },
    2: { text: '2', color: 'red' },
    3: { text: '3', color: 'blue' },
    4: { text: '4', color: 'orange' },
    5: { text: '5', color: 'cyan' },
    6: { text: '6', color: 'geekblue' },
    7: { text: '7', color: 'volcano' },
    8: { text: '8', color: 'magenta' },
    9: { text: '9', color: 'lime' },
  };

  columns: STColumn[] = [
    { title: '顺序', index: 'SEQ', type: 'tag', tag: this.TAG },
    { title: '权力编码', index: 'DAD004' },
    { title: '事项名称', index: 'DAD031' },
    { title: '所属部门', index: 'DGB010', dic: 'DGB010' },
    { title: '事项类型', index: 'DAD011', dic: 'DAD011' },
    { title: '对象主体', index: 'DAD032', type: 'badge', badge: this.BADGE },
    {
      title: '操作区', buttons: [
        {
          text: '调整',
          icon: 'edit',
          click: (record: any) => this.modi(record),
        },
        {
          text: '移除',
          icon: 'delete',
          click: (record: any) => this.del(record),
          pop: true,
          popTitle: '确认要将该子项移除吗？',
        },
      ]
    },
  ];

  addSchema: SFSchema = {
    properties: {
      DAD017: {
        type: 'string',
        title: '事项主键',
        ui:
        {
          hidden: true
        }
      }
    }
  };
  modiSchema: SFSchema = {
    properties: {
      DAD017: {
        type: 'string',
        title: '事项主键',
        ui:
        {
          hidden: true
        }

      },
      DAE001: {
        type: 'string',
        title: '事项选择',
        ui:
        {
          hidden: true
        }
      },
      DAD004: {
        type: 'string',
        title: '权力编码',
        ui: {
          widget: 'text',
          grid: {
            span: 24
          }
        }
      },
      DAD031: {
        type: 'string',
        title: '事项名称',
        ui: {
          widget: 'text',
          grid: {
            span: 24
          }
        }
      },
      SEQ: {
        type: 'number',
        title: '排列顺序',
        ui: {
          grid: {
            span: 24
          }
        }
      },

    },
    required: ["DAE001", "SEQ"],
    ui: {
      spanLabelFixed: 80,
      grid: {
        gutter: 20,
        span: 24
      }
    }
  };

  searchValue = '';
  gutter = 24;
  nodes: any = [];
  treeData: any;
  showModiWindow = false;
  showAddWindow = false;
  showUpdate = false;
  isAddDisabled = true;
  isDelDisabled = true;
  formData = {};
  modiFormData = {};
  updateFormData = {};
  isVisible = false;
  modalTitle = '添加子项';
  curDAD017 = 0;
  newDAD017


  @ViewChild('showSf', { static: false }) showSf: SFComponent;
  @ViewChild('addWindow', { static: false }) addWindow: SFComponent;
  @ViewChild('modiWindow', { static: false }) modiWindow: SFComponent;
  @ViewChild('oneadd', { static: false }) oneadd: SFComponent;
  @ViewChild('onemodi', { static: false }) onemodi: SFComponent;
  @ViewChild('btnSave', { static: false }) btnSave: NzButtonComponent;
  @ViewChild('st', { static: true }) st: GridComponent;
  constructor(private modal: ModalHelper, private lbs: HttpService, private supDic: SupDic, public msgSrv: NzMessageService, widgetRegistry: WidgetRegistry) {
    widgetRegistry.register(LbAAC001WidgetComponent.KEY, LbAAC001WidgetComponent);
  }

  // 初始化机构树
  initDeptTree() {
    this.lbs.lbservice('YJS_TREE', {}).then(res => {
      console.log(res.message.list);
      this.nodes = res.message.list;
    });
  }


  // 初始化事项级联
  getSx() {
    this.lbs.lbservice('YJS_SXJL', {}).then(res => {

      this.addSchema = {
        properties: {
          DAD017: {
            type: 'string',
            title: '事项主键',
            ui:
            {
              hidden: true
            }

          },
          DAE001: {
            type: 'string',
            title: '事项选择',
            enum: res.message.list,
            ui: 'cascader',
          },
          SEQ: {
            type: 'number',
            title: '排列顺序',
            ui: {
              grid: {
                span: 24
              }
            }
          },

        },
        required: ["DAE001", "SEQ"],
        ui: {
          spanLabelFixed: 80,
          grid: {
            gutter: 20,
            span: 24
          }
        }
      };






    });
    // return this.sfs;
  }

  // 功能树单击事件
  nzEvent(event: NzFormatEmitEvent): void {
    this.treeData = event.node.origin;
    // console.log(this.addSf);
    // console.log(event);
    if (this.treeData.isLeaf) {
      this.curDAD017 = this.treeData.key;
      // 编辑框表单赋值
      this.showSf.setValue('/DAD017', this.treeData.key);
      this.showSf.setValue('/DAD031', this.treeData.title);
      this.showSf.setValue('/DAD004', this.treeData.DAD004);
      this.showSf.setValue('/DGB010', this.treeData.DGB010);
      this.showSf.setValue('/DAD032', this.treeData.DAD032);
      // console.log(this.sf.getValue('/SEQ'));
      this.isAddDisabled = false;
      this.query();
    }

  }


  add() {
    console.log(this.getSx());
    this.formData = { DAD017: this.treeData.key }
    this.isVisible = true;
  }

  query() {

    this.st.reload({ DAD017: this.curDAD017 });
  }


  modi(rowData: any) {
    this.showModiWindow = true;
    this.modalTitle = '子项顺序调整';
    this.modiFormData = rowData;
  }

  del(rowData: any) {
    this.lbs.lbservice("YJS_ZXSC", { para: { DAD017: rowData.DAD017, DAE001: rowData.DAE001 } }).then(resdata => {
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.msgSrv.success('子项移除成功');
        this.query();
      }
    });
  }

  handleCancel() {
    this.isVisible = false;
    this.showAddWindow = false;
    this.formData = {};
  }

  handleOk() {
    if (!this.addWindow.valid) {
      this.msgSrv.error('必填项不能为空！');
      return;
    }

    this.lbs.lbservice('YJS_ZXXZ', { para: this.addWindow.value }).then(resdata => {
      console.log(this.addWindow.value);
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.isVisible = false;
        this.formData = {};
        this.msgSrv.success('子项添加成功');
        this.query();
      }

    });
  }

  modiHandleCancel() {
    this.showModiWindow = false;
    this.showUpdate = false;
  }

  modiHandleOk() {

    if (!this.modiWindow.valid) {
      this.msgSrv.error('必填项不能为空！');
      return;
    }

    this.lbs.lbservice('YJS_ZXXG', { para: this.modiWindow.value }).then(resdata => {
      console.log(this.modiWindow.value);
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.showModiWindow = false;
        this.modiFormData = {};
        this.msgSrv.success('子项顺序调整成功');
        this.query();
      }

    })

  }

  ngOnInit() {
    this.initDeptTree();
    this.getSx();
  }

  update() {
    this.lbs.lbservice('ZSK_ONEDECX', { para: { DAD017: this.curDAD017 } }).then(resdata => {
      console.log('更新',resdata);
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.updateFormData = resdata.message.list[0];
        this.showUpdate = true;
      }

    })

  }

  updateHandleOk() {
    if (!this.onemodi.valid) {
      this.msgSrv.error('必填项不能为空！');
      return;
    }

    this.lbs.lbservice('ZSK_SXXG', { para: this.onemodi.value }).then(resdata => {

      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.showUpdate = false;
        this.updateFormData = {};
        this.msgSrv.success('一件事修改成功');
        this.initDeptTree();
        this.curDAD017 = null;
        this.showSf.reset();
        this.st.st.reset();
      }

    })
  }
  confirm() {
    this.lbs.lbservice("ZSK_SXSC", { para: { DAD017: this.curDAD017 } }).then(resdata => {
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        // this.showAddWindow = false;
        // this.addFormData = {};
        this.msgSrv.success('一件事删除成功');
        this.initDeptTree();
        this.showSf.reset();
        this.curDAD017 = null;
        this.st.st.reset();
        /// this.query(true);
      }
    });

  }
  cancel() {

  }
  add1() {
    if (!this.oneadd.valid) {
      this.msgSrv.error('必填项不能为空！');
      return;
    }

    this.lbs.lbservice('ZSK_SXXZ', { para: this.oneadd.value }).then(resdata => {
      console.log(this.oneadd.value);
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.showAddWindow = false;
        this.updateFormData = {};
        this.msgSrv.success('一件事新增成功');
        this.initDeptTree();
      }

    })

  }
  addone(){
    this.showAddWindow = true
  }

}
